﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DC2010_Editor.Properties;
using DC2010.Tools;
using DC2010;
using DC2010.Objects;
using DC2010.Objects.Base;
using DC2010.Objects.Data;

namespace DC2010_Editor.Controls
{
    public partial class TileUC : UserControl
    {
        private TileData data { get; set; }

        public TileUC()
        {
            InitializeComponent();

            Refresh();
        }

        private void TileUC_Load(object sender, EventArgs e)
        {
            if (imageListBox1.Items.Count > 0)
            {
                imageListBox1.SelectedIndex = 0;
            }
            Refresh();
        }

        public void SetControls_Floor()
        {
            //Ids
            BasedOnTextBox.Text = data.InfoId;
            TileIdTextBox.Text = data.Id;

            itemsPlacementTextBox1.SetRelatedObject(data.Info.Items);
            seTextBox1.SetRelatedObject(data.Info.OnEnterEffects, EffectType.Instant);
            seTextBox2.SetRelatedObject(data.Info.OnLeaveEffects, EffectType.Instant);
            seTextBox3.SetRelatedObject(data.Info.Effects, EffectType.Pump);
            inventoryTextBox1.SetRelatedObject(data.Info.LinkedObjects);

            textBox16.Text = data.Info.WallSet;

            checkBox1.Checked = data.Info.IsWall;
            checkBox2.Checked = data.Info.CanMonsterPass;
            checkBox3.Checked = data.Info.CanPlayerPass;
            checkBox4.Checked = data.Info.CanHoldItems;

            //PartyTextBox.Text = data.Info.PartyId;

        }

        public void SetControls_Pit()
        {
            itemsPlacementTextBox1.SetRelatedObject(data.Info.Items);
            seTextBox1.SetRelatedObject(data.Info.OnEnterEffects, EffectType.Instant);
            seTextBox2.SetRelatedObject(data.Info.OnLeaveEffects, EffectType.Instant);
            seTextBox3.SetRelatedObject(data.Info.Effects, EffectType.Pump);
            //textBox3.Text = data.Info.WallSet;
        }

        public void GetFromControls_Floor()
        {
            data.Info.WallSet = textBox16.Text;
            data.Info.IsWall = checkBox1.Checked;
            data.Info.CanMonsterPass = checkBox2.Checked;
            data.Info.CanPlayerPass = checkBox3.Checked;
            data.Info.CanHoldItems = checkBox4.Checked;

            //data.Info.PartyId = PartyTextBox.Text;

            if (data is ClearFloorTile)
            {
                //do nothing more
            }

            if (data is DoorTile)
            {
                DoorTile dt = (DoorTile)data;

                if (comboBox1.Text == "NS") dt.Direction = DoorDirection.NS;
                if (comboBox1.Text == "WE") dt.Direction = DoorDirection.WE;
            }

            if (data is PitTile)
            {
                PitTile dt = (PitTile)data;

                dt.IsConcealed = checkBox6.Checked;
                dt.IsIllusionary = checkBox21.Checked;
                dt.IsOpen = checkBox22.Checked;
            }

            if (data is StairsTile)
            {
                StairsTile dt = (StairsTile)data;

                if (comboBox2.Text == "UP") dt.Direction = StairsDirection.Up;
                if (comboBox2.Text == "DOWN") dt.Direction = StairsDirection.Down;
            }
        }

        public new void Refresh()
        {
            string filter = textBox2.Text;

            imageListBox1.Items.Clear();

            foreach (Tile d in Root.I.TileSystem.ListInstance)
            {
//                Tile t = Root.I.TileSystem.ListInstance[250];
  //              BaseObject bo = t.FindOwner();

                BaseObject c = null;// d.FindOwner();

    //            c = bo;

                bool bOnlyCurrentMap = checkBox5.Checked;

                //if ((c is Map)&&(bOnlyCurrentMap))
                //{
                //    Map m = (Map)c;
                //    if (m.Info.Id == Editor.I.CurrentMapId) break;
                //}

                if (c == null)
                {
                    //if (!bOnlyCurrentMap)
                    {
                        ImageListBoxItem itm = new ImageListBoxItem(d.Info.Id, Resources.Instanced, d);
                    if (d.Info.Id.Contains(filter)) 
                        imageListBox1.Items.Add(itm);
                    }
                }
                else
                {
                        if ((c is Map)&&(bOnlyCurrentMap))
                        {
                            Map m  = (Map)c;
                            if (m.Info.Id == Editor.I.CurrentMapId)
                            {
                                ImageListBoxItem itm = new ImageListBoxItem(d.Info.Id+" ("+m.Info.Id+")", Resources.InstancedWithOwner, d);
                                if (d.Info.Id.Contains(filter))
                                    imageListBox1.Items.Add(itm);
                            }
                        }
                        else
                        {
                            ImageListBoxItem itm = new ImageListBoxItem(d.Info.Id, Resources.InstancedWithOwner, d);
                            if (d.Info.Id.Contains(filter))
                                imageListBox1.Items.Add(itm);
                        }
                
                    }
                
        
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (TileIdTextBox.Text.Contains("{0}"))
            {
                //noone was messing with that
                string id = string.Format(TileIdTextBox.Text, Editor.I.TilesCount);
                string tempId = textBox1.Text;

                ClearFloorTile cft = new ClearFloorTile();
                cft.Info.Id = id;
                cft.Info.TemplateId = tempId;

                Root.I.TileSystem.AddToSystem(cft, id, ObjectTarget.Core);
                Root.I.TileSystem.Save();

                Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (TileIdTextBox.Text.Contains("{0}"))
            {
                //noone was messing with that
                string id = string.Format(TileIdTextBox.Text, Editor.I.TilesInstanceCount);
                string tempId = textBox1.Text;

                ClearFloorTile cft = new ClearFloorTile();
                cft.Info.Id = id;
                cft.Info.TemplateId = tempId;

                Root.I.TileSystem.AddToSystem(cft, id, ObjectTarget.Instance);
                Root.I.TileSystem.SaveInstance();

                Refresh();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetFromControls_Floor();

            Root.I.TileSystem.Save();
            Root.I.TileSystem.SaveInstance();
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (imageListBox1.SelectedItem != null)
            {
                if (imageListBox1.SelectedItem is ImageListBoxItem)
                {
                    ImageListBoxItem ilbi = (ImageListBoxItem)imageListBox1.SelectedItem;

                    if (ilbi.Tag is ClearFloorTile)
                    {
                        data = (ClearFloorTile)ilbi.Tag;

                        if (data.Info.Items==null)
                            data.Info.Items = new List<DungeonPlacement>();

                        //switch to correct tab
//                        tabControl1.SelectedTab = FloorTabPage;
                        SetControls_Floor();
                    }
                    if (ilbi.Tag is PitTile)
                    {
                        data = (PitTile)ilbi.Tag;

                        if (data.Info.Items == null)
                            data.Info.Items = new List<DungeonPlacement>();

                        //switch to correct tab
                        tabControl1.SelectedTab = PitTabPage;
                        SetControls_Pit();
                    }

                    if (ilbi.Tag is Tile)
                    {
                        data = (Tile)ilbi.Tag;
                    }
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //Root.I.TileSystem.LoadInstance();
            Refresh();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            Refresh();
        }

        private void crystalButton7_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                data.Id = data.Info.Id;
                Root.I.TileSystem.RemoveFromSystem(data, ObjectTarget.Core);
                Root.I.TileSystem.RemoveFromSystem(data, ObjectTarget.Instance);
            }

            Refresh();

        }
    }
}
